﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Watcher
{
    public partial class ShowMap : Form
    {
        public ShowMap(int zoneid, int level, int x, int y)
        {

            _zoneId = zoneid;
            _x = x;
            _y = y;
            _level = level;
            InitializeComponent();

        }

        private int _zoneId;
        private int _x;
        private int _y;
        private int _level; 

        public Image DownloadImage(string _URL)
        {
            Image _tmpImage = null;

            try
            {
                // Open a connection
                System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);

                _HttpWebRequest.AllowWriteStreamBuffering = true;

                // You can also specify additional header values like the user agent or the referer: (Optional)
                _HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
                _HttpWebRequest.Referer = "http://www.google.com/";

                // set timeout for 20 seconds (Optional)
                _HttpWebRequest.Timeout = 20000;

                // Request response:
                System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();

                // Open data stream:
                System.IO.Stream _WebStream = _WebResponse.GetResponseStream();

                // convert webstream to image
                _tmpImage = Image.FromStream(_WebStream);

                // Cleanup
                _WebResponse.Close();
                _WebResponse.Close();
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
                return null;
            }

            return _tmpImage;
        }

        public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
        {
            //a holder for the result
            Bitmap result = new Bitmap(width, height);

            //use a graphics object to draw the resized image into the bitmap
            using (Graphics graphics = Graphics.FromImage(result))
            {
                //set the resize quality modes to high quality
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //draw the image into the target bitmap
                graphics.DrawImage(image, 0, 0, result.Width, result.Height);
            }

            //return the resulting bitmap
            return result;
        }


        private void ShowMap_Load(object sender, EventArgs e)
        {
            try
            {
                Image _Image = null;
                StringBuilder sb = new StringBuilder();

                if (_level > 0)
                {
                    _level--;
                    sb.Clear();
                    sb.Append("http://wow.zamimg.com/images/wow/maps/enus/normal/");
                    sb.Append(_zoneId.ToString());
                    sb.Append("-");
                    sb.Append(_level.ToString());
                    sb.Append(".jpg");
                }
                else
                {
                    sb.Clear();
                    sb.Append("http://wow.zamimg.com/images/wow/maps/enus/normal/");
                    sb.Append(_zoneId.ToString());
                    sb.Append(".jpg");
                }

                _Image = DownloadImage(sb.ToString());
                _Image = ResizeImage(_Image, 300, 300);

                using (Graphics g = Graphics.FromImage(_Image))
                {
                    g.DrawEllipse(new Pen(Color.Red, 3), _x, _y, 2, 2);
                }

                pictureBox1.Image = _Image;
            }
            catch (Exception ex)
            {
                label1.Text = "Unable to get Map sry :(";
            }
        }
    }
}
